package net.ubinow.mflex.events;

import net.ubinow.mflex.data.IDataItem;

public class Event {
	 /** used when registering the listener; this indicates that all events should be dispatched to the listener **/
    public static final int EVT_ANY             = 0; 
    
    public static final int EVT_INDEX_CHANGED   = 1;    
    public static final int EVT_ON_FOCUS        = 2;
    public static final int EVT_OFF_FOCUS       = 3;
    public static final int EVT_ITEM_ADDED      = 4;
    public static final int EVT_ITEM_REMOVED    = 5; 
    
    ///
    // transitional events
    ///
    public static final int EVT_TRANSITION_IN_FINISHED  = 20;
    public static final int EVT_TRANSITION_OUT_FINISHED = 21; 
    
    ///
    // IView events 
    ///
    public static final int EVT_VIEW_SHOWING            = 30;
    public static final int EVT_VIEW_HIDING             = 31;
//    public static final int EVT_VIEW_HIDDEN             = 32; 
    
    ///
    // AbsWidget events 
    ///
    public static final int EVT_SELECT_DOWN             = 40;
    public static final int EVT_TEXT_CHANGED            = 41;
    public static final int EVT_GUI_OUT_OF_BOUNDS       = 42;
    public static final int EVT_GUI_RESIZE              = 43;
    public static final int EVT_OFFSET_Y_CHANGE         = 44;  
    
    // generic events
    public static final int EVT_UNDEFINED				= 50;
    
    
    // EVT_OFF_FOCUS is also used by the widget container and is that event that are fired from the WidgetContainer when _loopingWidgetNav is
    // set to false and the focus has reached either the top or bottom - this is to allow it's container to be notified that
    // it now has the focus - or should the focus
    
    ///
    // CommandBar Events 
    ///
    public static final int EVT_CMD_SELECTED            = 50;
    
    public boolean _inUse = false;
    
    public int _currentGetDataIndex = 0; // index to the next item in the _result set to be retrieved
    public IDataItem[] _result = null;
	public Object _value = null;
	public int _event 		 = EVT_UNDEFINED;
	
	// TODO: TYPE_SIMPLE, TYPE_REQUEST (?)
	public int _type = -1; 
	
	public IEventPublisher _publisher = null;
    
	public boolean is_inUse() {
		return _inUse;
	}

	public void set_inUse(boolean use) {
		_inUse = use;
	}
	
	
	public void init(){
		set_inUse(true); 	
	}
	
	

	public IDataItem getDataItem() {
		if( _currentGetDataIndex == -1 || _result == null )
			return null;
		
		IDataItem data = _result[_currentGetDataIndex];
		
		// work out the next index value we should return
		_currentGetDataIndex--;	
		
		if( _currentGetDataIndex < 0 )
			_currentGetDataIndex = _result.length;
		
		return data; 
	}
	
	public IDataItem[] getDataItems(){
		return _result; 
	}		
	
	public void setDataItem( IDataItem item ){
		_result = new IDataItem[]{ item }; 
	}
	
	public void setDataItems( IDataItem[] itms ){
		if( itms == null ){	
			_currentGetDataIndex = -1;
			return; 
		}
		
	    _result = itms;
	    _currentGetDataIndex = itms.length-1;
	}
	
	
	public void release()
	{
		_event = EVT_UNDEFINED;
		_publisher = null;
		_result = null;
		_inUse = false; 		
	}
    
	public IEventPublisher getPublisher()
	{
		return _publisher; 
	}
	
	public void setPublisher( IEventPublisher publisher ){
		_publisher = publisher;
	}

	public int getEventID()
	{
		return _event; 
	}
	
	public void setEventID( int event ){
		_event = event;
	}
    
}
